import { createRouter, createWebHashHistory } from 'vue-router'

import { routes, commonPath } from './routes'
import { verifyJurisdiction } from './help.js'
import { whitelist } from './routerConfig.js'
const router = createRouter({
  history: createWebHashHistory(),
  // base: import.meta.env.BASE_URL,
  routes,
})

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  let userInfo = localStorage.getItem('USERSTORE')
  if (to.path == '/') {
    router.replace({
      path: commonPath().home,
    })
  }

  if (token && userInfo) {
    if (to.fullPath !== '/' && !router.hasRoute(to.name)) {
      // BuriedPoint(to, from, next, `/404`)
    } else {
      verifyJurisdiction(to, from, next)
    }
  } else {
    if (whitelist.indexOf(to.path) > -1) {
      next()
    } else {
      // next('/login')
      next()

    }
  }
})
// 全局后置路由守卫
router.afterEach((to) => {
  // document.title = to.meta.title || ''
})

export default router
